package com.example.weather_app.diary;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CalendarView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.weather_app.R;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class DiaryActivity extends AppCompatActivity {
    private CalendarView calendarView;
    private RecyclerView recyclerView;
    private FloatingActionButton fabAddDiary;
    private DiaryAdapter diaryAdapter;
    private DiaryDao diaryDao;
    private String selectedDate;
    private SimpleDateFormat dateFormat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.diary_activity);

        // 设置Toolbar
        androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle("我的日记");
        }

        // 初始化组件
        calendarView = findViewById(R.id.calendarView);
        recyclerView = findViewById(R.id.recyclerDiaries);
        fabAddDiary = findViewById(R.id.fabAddDiary);
        diaryDao = new DiaryDao(this);
        dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);

        // 设置RecyclerView
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        diaryAdapter = new DiaryAdapter(this);
        recyclerView.setAdapter(diaryAdapter);

        // 获取今天的日期
        Date today = new Date();
        selectedDate = dateFormat.format(today);
        loadDiariesForDate(selectedDate);

        // 日历选择事件
        calendarView.setOnDateChangeListener((view, year, month, dayOfMonth) -> {
            // 月份从0开始，需要+1
            selectedDate = year + "-" + String.format("%02d", month + 1) + "-" + String.format("%02d", dayOfMonth);
            loadDiariesForDate(selectedDate);
        });

        // 添加日记按钮点击事件
        fabAddDiary.setOnClickListener(v -> {
            Intent intent = new Intent(DiaryActivity.this, AddDiaryActivity.class);
            intent.putExtra("date", selectedDate);
            startActivity(intent);
        });
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.root_layout), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }

    // 加载指定日期的日记
// DiaryActivity.java
    private void loadDiariesForDate(String date) {
        Log.d("DiaryActivity", "Loading diaries for date: " + date);
        List<Diary> diaries = diaryDao.getDiariesByDate(date);
        Log.d("DiaryActivity", "Number of diaries loaded: " + diaries.size());
        diaryAdapter.setDiaries(diaries);
    }

    // 刷新日记列表
    private void refreshDiaries() {
        loadDiariesForDate(selectedDate);
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshDiaries();
    }

    // 菜单创建
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.diary_menu, menu);
        return true;
    }

    // 菜单点击事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_reminders) {
            Intent intent = new Intent(this, ReminderListActivity.class);
            startActivity(intent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}